#!/usr/bin/env python
import os


def prepend(fileName, pre, pos):
    fp = open(fileName, "w")
    fp.write(pre)
    fp.write(pos)
    fp.close()


def getHeader():
    fp = open("../header")
    buf = fp.read()
    fp.close()
    
    return buf


def goThrowDirs(root):
    header = getHeader()
    
    topheader = header[1:30]
    
    for root, dirs, files in os.walk(root):
	if root.find(".svn") >= 0:
	    continue
	    
	for f in files:
	    file = root + "/" + f
	    if (file.find(".cpp") > 0) or (file.find(".h") > 0):
		srcfp = open(file)
		src = srcfp.read()
		srcfp.close()
		if src.find(topheader) == -1:
		    backfp = open(file+".bak","w")
		    backfp.write(src)
		    backfp.close()
		    
		    prepend(file, header, src)
		    
		    #print file + " - header placement"
	
	
if __name__ == '__main__':	    
    goThrowDirs("../src/")
    goThrowDirs("../include/")
		    
	
	